package organizer.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import organizer.main.Organizer;

public class WeekOfYearPanel extends JPanel {

	GridBagConstraints gbc = null;
	JLabel weekOfYearLabel = null;

	private int buttonSkipWeeks = 1;
	private UI ui = null;

	private int getWeekOfYear() {
		return ui.getOrganizer().getWeekOfYear();
	}

	public WeekOfYearPanel(UI ui) {
		this.ui = ui;
		gbc = new GridBagConstraints();

		init();
		addComponents();

//		updateWeekAndLabel(getWeekOfYear());
	}

	private void init() {
		this.setBackground(Color.yellow);
		this.setLayout(new GridBagLayout());
	}

	private void addComponents() {
		JButton prevWeek = new JButton("<");
		prevWeek.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				updateWeekAndLabel(getWeekOfYear() - buttonSkipWeeks);
			}
		});
		gbc.gridx = gbc.gridy = 0;
		// gbc.gridwidth = gbc.gridheight = 1;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		this.add(prevWeek, gbc);

		weekOfYearLabel = new JLabel();
		gbc.gridx++;
		gbc.gridwidth = 2;
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		this.add(weekOfYearLabel, gbc);

		JButton nextWeek = new JButton(">");
		nextWeek.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				updateWeekAndLabel(getWeekOfYear() + buttonSkipWeeks);
			}
		});
		gbc.gridx += gbc.gridwidth;
		gbc.gridwidth = 1;
		gbc.anchor = GridBagConstraints.NORTHEAST;
		gbc.fill = GridBagConstraints.NONE;
		this.add(nextWeek, gbc);
	}

	private void updateWeekAndLabel(int weeks) {
		ui.getOrganizer().loadWeek(weeks);
		updateLabelText();
		ui.updatePanels();
	}

	private void updateLabelText() {
		weekOfYearLabel.setText("KW: " + getWeekOfYear());
	}

	// @Override
	// public void paintComponent(Graphics g) {
	// Organizer o = Organizer.getInstance();
	// if (o != null && weekOfYearLabel != null) {
	// }
	// }
}
